import bs4
import re
import urllib.request
import urllib.error
import xlwt
import xlrd
from openpyxl import Workbook
from openpyxl import load_workbook
from openpyxl.writer.excel import ExcelWriter


# 创建正则表达式对象
findLink = re.compile(r'<p>(.*)</p>')


# 主函数
def main():
    baseurl = "https://www.lishixinzhi.com/lishishuji/1271559.html"
    datalist = getData(baseurl)
    savepath = r"C:\Users\dingyanfeng\Desktop\homework\test.xlsx"
    saveData(datalist, savepath)


def getData(baseurl):
    print("开始")
    datalist = []
    for i in range(0, 1): 
        url = baseurl
        html = askURL(url)
        # print(html)
        # 解析数据
        soup = bs4.BeautifulSoup(html, "html.parser")
        for item in soup.find_all('div', class_="article_content"):
            # print(item)    # 测试item
            data = []  
            item = str(item)

            link = re.findall(findLink, item)[0]
            data.append(link)
            data[0] = data[0].split('</p><p>')
            datalist.append(data)

    result_temp = datalist[0][0]
    # 处理数据得到姓名
    result = []
    for astr in result_temp:
        index1 = astr.find('.')
        index2 = astr.find('-')
        if index2 < 0:
            index2 = astr.find("字")
        if 0 < index1 < index2 and index2 > 0:
            if len(astr[index1+1:index2]) <= 3:
                result.append(astr[index1+1:index2])
    result = result[:200]
    # print(result)  # 测试数据
    return result


# 获得一个URL的网页内容
def askURL(url):
    # 模拟浏览器头部信息，向服务器发送消息(该信息需要自己获得)
    print("爬取中...")
    head = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64)\
AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.80 Safari/537.36 Edg/98.0.1108.50"}
    request = urllib.request.Request(url, headers=head)
    html = ""
    try:
        response = urllib.request.urlopen(request)
        html = response.read().decode('utf-8')
        # print(html)
    except urllib.error.URLError as e:
        if hasattr(e, "code"):
            print(e.code)
        if hasattr(e, "reason"):
            print(e.reason)
    return html


# 保存数据到excel
def saveData(datalist, savepath):
    print("保存中...")
    book = load_workbook(savepath)
    # book = xlwt.Workbook(encoding='utf-8', style_compression=0)
    sheet_name = 'Sheet1'  # 获得表单名字
    sheet = book[sheet_name]
    # 保存数据
    for i in range(0, len(datalist)):
        # print("第%d部" % (i+1))
        data = datalist[i]
        sheet['C%d' % (i+1)] = data
        # sheet.write(i, 2, data)

    book.save(savepath)
    print("完成")


if __name__ == "__main__":
    main()


